<?php
require_once dirname(__FILE__) . '/common.inc.php';

if (isset($_COOKIE[LANG])) {
  $language = $_COOKIE[LANG];
} else {
  $language = 'en';
}
$smarty->assign('lang', $language);

// I18N support information here
putenv("LANG=$language"); 
setlocale(LC_ALL, $language);

// Set the text domain as 'messages'
$domain = 'messages';
bindtextdomain($domain, dirname(__FILE__). '/locale'); 
bind_textdomain_codeset($domain, "utf-8");
textdomain($domain);


if (isset($_COOKIE[USER])) {
  $_SESSION[USER] = json_decode($_COOKIE[USER]);
}

$template = MiscUtils::getParam('t', 'signin');
$options = MiscUtils::getParam('opts', json_encode(array()));
$base = MiscUtils::getParam('b', 'base');

$smarty->assign('p', 'admin/gui/html');
$smarty->assign('u', json_encode(array()));
$smarty->assign('d', json_encode(array()));
$smarty->assign('opts', json_encode(array()));
$smarty->assign('msg', json_encode(array()));

$logout = MiscUtils::getParam('logout', NULL);
if ($logout === '') {
  unset($_SESSION[USER]);
  setcookie(USER, '', time() - 1, PATH, DOMAIN);
  $smarty->assign('t', 'signin');
  $smarty->display("admin/gui/baseblank.tpl");
  return; /* SignOut */
}

if (isset($_SESSION[USER])) {
  if ($template == 'signin') {
    $template = 'pages';
  }
  $smarty->assign('user', $_SESSION[USER]);
  $smarty->assign('t', "$template");
  $smarty->assign('u', json_encode($_SESSION[USER]));
  $smarty->assign('d', time());
  $smarty->assign('opts', urldecode($options));
  
  $message = array();
  $message['plugins'] = DbUtils::get(DbUtils::createPdoInst(), 'pz_plugin', NULL, NULL, NULL, NULL, NULL, NULL);
  loadPage($template, $message);
  $smarty->display("admin/gui/$base.tpl");
  return;
} else {
  $smarty->assign('t', 'signin');
  $smarty->display("admin/gui/baseblank.tpl");
  return; /* SignIn Required */
}

function loadPage($template, $message) {
  /**NOT GOOD*/
  global $smarty;
  
  $pages = new stdClass();
  $pages->id = 0;
  $pages->name = 'Pages';
  $pages->link = 'admin.php?t=pages';
  $pages->c = array();
  _pages(DbUtils::get(DbUtils::createPdoInst(), 'pz_menu', NULL, NULL, NULL, NULL, NULL, NULL)->d, $pages);
  
  $tmpls = new stdClass();
  $tmpls->id = -1;
  $tmpls->name = 'Page Templates';
  $tmpls->link = 'admin.php?t=pages';
  $tmpls->c = array();
  
  $themes = new stdClass();
  $themes->id = -1;
  $themes->name = 'Site Themes';
  $themes->link = 'admin.php?t=pages';
  $themes->c = array();
  
  $plgns = new stdClass();
  $plgns->id = -1;
  $plgns->name = 'Global Plugin Settings';
  $plgns->link = 'admin.php?t=pages';
  $plgns->c = array();
  
  $profl = new stdClass();
  $profl->id = -1;
  $profl->name = 'Profile';
  $profl->link = 'admin.php?t=user&i=1';
  $profl->c = array();
  
  $store = new stdClass();
  $store->id = -1;
  $store->name = 'Our Store';
  $store->link = 'admin.php?t=user&i=1';
  $store->c = array();
  
  $suprt = new stdClass();
  $suprt->id = -1;
  $suprt->name = 'Support';
  $suprt->link = 'admin.php?t=user&i=1';
  $suprt->c = array();
  
  $menuInst = new stdClass();
  $menuInst->id = -1;
  $menuInst->name = 'Root';
  $menuInst->link = '';
  $menuInst->c = array($pages, $tmpls, $themes, $plgns, $profl, $store, $suprt);
  $smarty->assign('menuInst', $menuInst);
  
  switch ($template) {
    case 'pages':
      pages($message);
      break;
    case 'page':
      page($message);
      break;
    case 'user':
      user($message);
      break;
    default:
      header('Location: http://' . DOMAIN . PATH);
      break;
  }
}

function _pages($arr, $node) {
  for ($i = 0, $il = count($arr); $i < $il; $i++) {
    $tmp = $arr[$i];
    if ($tmp->parent_id == $node->id) {
      $tmp->link = 'admin.php?b=baseblank&t=page&i=' . $tmp->id;
      $tmp->c = array();
      $node->c[] = $tmp;
      _pages($arr, $tmp);
    }
  }
}

function user($message) {
  global $smarty;
  $id = MiscUtils::getParam('i', NULL);
  if ($id) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.id= :v1';
    $cond_vals->v = array(':v1' => $id);
    $message[0] = DbUtils::get(DbUtils::createPdoInst(), 'pz_user', $cond_vals, NULL, NULL, NULL, NULL, NULL);
    $smarty->assign('msg', json_encode($message));
  }
}

function pages($message) {
  global $smarty;
  $message['m'] = MiscUtils::createMenuInst(DbUtils::get(DbUtils::createPdoInst(), 'pz_menu', NULL, 't.order', 'ASC', NULL, NULL, NULL)->d, NULL);
  $smarty->assign('msg', json_encode($message));
}

function page($message) {
  global $smarty;
  $id = MiscUtils::getParam('i', NULL);
  if ($id) {
    $smarty->assign('menu_id', $id); 
    
    $cond_vals = new stdClass();
    $cond_vals->c = 't.menu_id = :v1';
    $cond_vals->v = array(':v1' => $id);
    $message[0] = DbUtils::get(DbUtils::createPdoInst(), 'pz_page', $cond_vals, NULL, NULL, NULL, NULL, NULL);
    $message['i'] = $id;
    
    $cond_vals = new stdClass();
    $cond_vals->c = 't.menu_id = :v1';
    $cond_vals->v = array(':v1' => $id);
    $opt = new stdClass();
    $opt->select_expr = 't.*, t1.name, t1.folder';
    $opt->join_expr = 'LEFT JOIN pz_plugin AS t1 ON t1.id = t.plugin_id';
    $instances = DbUtils::get(DbUtils::createPdoInst(), 'pz_plugin_instance', $cond_vals, NULL, NULL, NULL, NULL, $opt);
    $message['instances'] = $instances;
    
    $smarty->assign('msg', json_encode($message));
  }
}
?>